package com.exsun.ex1077.ui.activity.feedback

import android.os.Bundle
import com.blankj.utilcode.util.RegexUtils
import com.drake.brv.annotaion.DividerOrientation
import com.drake.brv.utils.bindingAdapter
import com.drake.brv.utils.divider
import com.drake.brv.utils.grid
import com.drake.brv.utils.setup
import com.exsun.ex1077.R
import com.exsun.ex1077.databinding.ActivityFeedbackBinding
import com.zhangping.fastjetpack.base.BaseActivity
import com.zhangping.fastjetpack.extend.view.afterTextChange
import com.zhangping.fastjetpack.extend.base.parseState
import com.zhangping.fastjetpack.extend.showToast
import com.zhangping.fastjetpack.utils.GlideEngine
import com.luck.picture.lib.PictureSelector
import com.luck.picture.lib.config.PictureMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.listener.OnResultCallbackListener
import com.lxj.xpopup.XPopup
import com.zhangping.fastjetpack.extend.selectPhoto
import kotlinx.android.synthetic.main.activity_feedback.*

/**
 * 描述：意见反馈
 * 作者：ZhangPing
 * 创建时间：2022/8/11 10:19
 */
class FeedbackActivity: BaseActivity<FeedbackViewModel, ActivityFeedbackBinding>() {

    override fun title() = "意见反馈"

    private val maxSelectNum = 3

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.vm = mViewModel
        super.initView(savedInstanceState)
        photoListView.grid(3).divider {
            orientation = DividerOrientation.GRID
            setDivider(10, true)
        }.setup {
            addType<String> { if (this == "add") R.layout.list_item_upload_add else R.layout.list_item_upload_photo }
            onClick(R.id.addPhoto, R.id.photo, R.id.delete){
                when (it){
                    R.id.addPhoto -> {
                        //添加照片
                        selectPhoto(maxSelectNum - photoListView.bindingAdapter.mutable.count { it != "add" }){
                            val list = photoListView.bindingAdapter.mutable
                            list.remove("add")
                            list.addAll(it)
                            if (list.size < maxSelectNum) list.add("add")
                            notifyDataSetChanged()
                        }
                    }
                    R.id.photo -> {
                        //预览照片
                    }
                    R.id.delete -> {
                        //删除照片
                        mutable.removeAt(modelPosition)
                        notifyItemRemoved(modelPosition)
                        if (!mutable.contains("add")){
                            mutable.add("add")
                            notifyDataSetChanged()
                        }
                    }
                }
            }
        }.models = arrayListOf("add")

        contentEdit.afterTextChange {
            contentLenghth.text = "${it.length}/500"
        }

        submit.setOnClickListener {
            if (mViewModel.content.get().isEmpty()){
                "问题/意见不能为空".showToast()
                return@setOnClickListener
            }
            telEdit.text.toString().let {
                if (it.isEmpty()) return@let
                if (!RegexUtils.isMobileExact(it)){
                    "请输入正确的手机号".showToast()
                    return@setOnClickListener
                }
            }
            mViewModel.submitEnable.set(false)
            val pathList = photoListView.bindingAdapter.models?.filter { it != "add" }
            if (!pathList.isNullOrEmpty()){
                //先上传图片
                mViewModel.uploadPhoto(pathList as List<String>).observe(this){
                    mViewModel.submitFeedback(it)
                }
                return@setOnClickListener
            }
            mViewModel.submitFeedback(null)
        }
    }

    override fun createObserver() {
        super.createObserver()
        mViewModel.submitResult.observe(this){
            parseState(it, {
                "提交成功".showToast()
                finish()
            }, {
                mViewModel.submitEnable.set(true)
            })
        }
    }

}